package util.io;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

public class FileOutput {

	/**
	 * @param args
	 */

	OutputStreamWriter writer;

	OutputStreamWriter writer2;

	private boolean eof = false;

	FileWriter outFile;

	PrintWriter out;

	private int window = 0;

	ArrayList data;

	String path;

	public FileOutput(ArrayList data, String path) {

		this.data = data;
		this.path = path;

	}
	
	public FileOutput( String path) {

		
		this.path = path;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String path = "c:\\Training.txt";

	}

	
	public void createFile(String file, String text){
		 OutputStreamWriter writer=null;
		try {
			
		    OutputStream fout= new FileOutputStream(file);

			OutputStream bout= new BufferedOutputStream(fout);
	        writer  = new OutputStreamWriter(bout, "utf8");
					
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try {
			writer.write(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	public void createBigramFile(Hashtable<String, Integer> c,
			Hashtable<String, Hashtable<String, Integer>> p, double N) {

		Enumeration<Integer> pair;

		try {
			writer = new OutputStreamWriter(new FileOutputStream(new File(path
					+ ".txt")), "ISO8859_1");

			writer2 = new OutputStreamWriter(new FileOutputStream(new File(path
					+ "JOIN.txt")), "ISO8859_1");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Enumeration<String> i = p.keys();

		Double sum = 0.0;

		Integer hi;

		String ci;

		sum = 0.0;
		Enumeration<String> k;
		while (i.hasMoreElements()) {
			ci = i.nextElement();

			pair = p.get(ci).elements();

			// System.out.println("Procesa " + ci);

			while (pair.hasMoreElements()) {

				hi = pair.nextElement();
				// System.out.println(ck);
				sum = ((double) hi / (double) c.get(ci));
				try {
					writer.write(String.valueOf(sum) + "\n");
					writer2.write(String.valueOf((double) (hi / N)) + "\n");

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//	sum = sum+ Math.log((double) ((double) hi / (double) c.get(ci)))/ Math.log(2.0);

			}

		}

		try {
			writer.close();
			writer2.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void create_setData() {

		int count = 0;

		try {
			writer = new OutputStreamWriter(
					new FileOutputStream(new File(path)), "ISO8859_1");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Iterator i = data.iterator();
		String tmp = "";
		while (i.hasNext()) {
			tmp = (String) i.next();

			try {
				writer.write(tmp + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void createTestDate(String path) {
		int count = 0;

		try {
			writer = new OutputStreamWriter(
					new FileOutputStream(new File(path)), "ISO8859_1");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Object[] da = data.toArray();
		int i = 0;
		for (i = data.toArray().length - 20000; i < data.toArray().length; i++) {

			try {
				count++;
				writer.write((String) da[i] + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		System.out.println("lines:" + count);

		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void createHeldDate(String path) {
		int count = 0;

		try {
			writer = new OutputStreamWriter(
					new FileOutputStream(new File(path)), "ISO8859_1");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Object[] da = data.toArray();

		for (int i = data.toArray().length - 1 - 60000; i < data.toArray().length - 1 - 20000; i++) {

			try {
				count++;
				writer.write((String) da[i] + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		System.out.println("lines:" + count);
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void createTrainingDate(String path) {
		int count = 0;

		try {
			writer = new OutputStreamWriter(
					new FileOutputStream(new File(path)), "ISO8859_1");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Object[] da = data.toArray();

		for (int i = 0; i < data.toArray().length - 2 - 60000; i++) {

			try {
				count++;
				writer.write((String) da[i] + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		System.out.println("lines:" + count);
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void createAlphaFile(Hashtable<String, Integer> alphab) {
		// TODO Auto-generated method stub

		Enumeration<String> a = alphab.keys();

		try {
			writer = new OutputStreamWriter(
					new FileOutputStream(new File(path)), "ISO8859_1");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String tmp;
		while (a.hasMoreElements()) {
			tmp = a.nextElement();

			try {
				writer.write(tmp + "\t" + alphab.get(tmp) + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		try {
			writer.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
